package logic

import (
	"context"
	"fmt"
	"k8s.io/apimachinery/pkg/util/rand"
	"yuni/model/mysql"

	"yuni/yunirpc/internal/svc"
	"yuni/yunirpc/yunirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *yunirpc.RegisterRequest) (*yunirpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.Db.Debug().Where("account=?", in.Account).Find(&user)
	if user.ID != 0 {
		return nil, fmt.Errorf("账号已存在")
	}
	m := mysql.User{
		Account:  in.Account,
		Password: in.Password,
		Image:    "https://ts1.tc.mm.bing.net/th/id/OIP-C.7GLMYPqMlt2LgkbPsOnDIAAAAA?w=207&h=211&c=8&rs=1&qlt=70&o=7&cb=thws4&dpr=1.5&pid=3.1&rm=3",
		Name:     fmt.Sprintf("默认名称%v%v", rand.String(5), rand.Intn(10)),
	}
	mysql.Db.Debug().Create(&m)

	return &yunirpc.RegisterResponse{
		UserId: int64(m.ID),
	}, nil
}
